package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.nhc.database.dbmodel.Measure;
import com.nhc.database.manager.MeasureCenter;
import com.nhc.model.MapSetInfo;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.BottomMenuWindow;
import zuo.biao.library.ui.EditTextInfoWindow;

public class MeasureSettingActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "MeasureSettingActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, MeasureSettingActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.measuresetting_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private ScrollView svDemoMain;

    private TextView tv_measuretype;
    private TextView tv_width;
    private TextView tv_slopeangle;

    @Override
    public void initView() {//必须调用
        svDemoMain = (ScrollView) findViewById(R.id.svDemoMain);

        tv_measuretype = (TextView) findViewById(R.id.tv_measuretype);
        tv_width = (TextView) findViewById(R.id.tv_width);
        tv_slopeangle = (TextView) findViewById(R.id.tv_slopeangle);

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Measure _measure;

    @Override
    public void initData() {//必须调用
        _measure = MeasureCenter.getInstance().measureEdit;
        refresh();
    }

    void refresh() {
        if (_measure == null) {
            return;
        }

        findViewById(R.id.mapset_1).setVisibility(View.GONE);
        findViewById(R.id.mapset_2).setVisibility(View.GONE);

        String strAreatype = getString(R.string.ssmjcl);
        switch (_measure.getAreatype()) {
            default:
                break;
            case 1:
                strAreatype = getString(R.string.dkmjcl);
                findViewById(R.id.mapset_1).setVisibility(View.VISIBLE);
                break;
            case 2:
                strAreatype = getString(R.string.pmcl);
                findViewById(R.id.mapset_2).setVisibility(View.VISIBLE);
                break;
        }
        tv_measuretype.setText(strAreatype);

        String WITH[] = SysUtils.getInstance().getDistanceStr((int) _measure.getWidth());
        tv_width.setText(WITH[0] + WITH[1]);
        tv_slopeangle.setText((int) _measure.getSlopeangle() + "");
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.mapset_0).setOnClickListener(this);
        findViewById(R.id.mapset_1).setOnClickListener(this);
        findViewById(R.id.mapset_2).setOnClickListener(this);
    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应
//        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu)), REQUEST_TO_TOP_MENU, false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mapset_0:
                toActivity(BottomMenuWindow.createIntent(context, getResources().getStringArray(R.array.area_mesure))
                        .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.mapdir)), REQUEST_TO_BOTTOM_MENU_1, false);
                break;
            case R.id.mapset_1:
                onSetWidth();
                break;
            case R.id.mapset_2:
                onSetSlopeangle();
                break;
            default:
                break;
        }
    }

    private void onSetWidth() {
        intent = EditNumberActivity.createIntentD(context
                , getString(R.string.szkd), _measure.getWidth(), getString(R.string.meter));
        toActivity(intent, REQUEST_TO_WIDTH);
    }

    private void onSetSlopeangle() {
        intent = EditNumberActivity.createIntent(context
                , getString(R.string.pdj), (int) _measure.getSlopeangle(), getString(R.string.du));
        toActivity(intent, REQUEST_TO_SLOPA);
    }

    private final int REQUEST_TO_BOTTOM_MENU_1 = 20;
    private final int REQUEST_TO_WIDTH = 23;
    private final int REQUEST_TO_SLOPA = 30;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU_1:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    _measure.setAreatype(selectedPosition);
                    refresh();
                }
                break;
            case REQUEST_TO_WIDTH:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditTextInfoWindow.RESULT_VALUE);
                    _measure.setWidth(Double.valueOf(editedValue));
                    refresh();
                }
                break;
            case REQUEST_TO_SLOPA:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditTextInfoWindow.RESULT_VALUE);
                    _measure.setSlopeangle(Integer.valueOf(editedValue));
                    refresh();
                }
                break;
        }
    }


    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {

    }
    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
